package com.example.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.dellpc.myapplication.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by 刘智强 on 2016/9/19.
 */
public class CustomPtrHeader extends FrameLayout implements PtrUIHandler {

    private TextView mtv_refresh;
    private ImageView miv_bird;
    //    private TranslateAnimation translateAnimation;
    private ObjectAnimator oa;
    private Animation animation;

    public CustomPtrHeader(Context context) {
        super(context);
        init();
    }

    public CustomPtrHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomPtrHeader(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    public void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_customptrheader, this);
        mtv_refresh = (TextView) view.findViewById(R.id.tv_refresh);
        miv_bird = (ImageView) view.findViewById(R.id.mpb_refresh);
        initAnimation();
    }

    public void initAnimation() {
//        translateAnimation= (TranslateAnimation) AnimationUtils.loadAnimation(getContext(),R.anim.bird_animation);
//        oa = ObjectAnimator.ofFloat(miv_bird, "translationX", 0f,250f);
//        oa.setDuration(1000);
//        oa.setRepeatMode(ObjectAnimator.RESTART);
//        animation=AnimationUtils.loadAnimation(getContext(),R.anim.bird_animation);
        animation = new TranslateAnimation(0, 250, 0, 0);
        animation.setDuration(2000);
//        animation.setInterpolator(getContext(),android.R.anim.cycle_interpolator);
        animation.setRepeatMode(Animation.RESTART);
        animation.setRepeatCount(3);
    }

    @Override
    public void onUIReset(PtrFrameLayout ptrFrameLayout) {

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
        mtv_refresh.setText("下拉刷新...");


    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout ptrFrameLayout) {
        mtv_refresh.setText("开始刷新...");
//        oa.start();
        miv_bird.startAnimation(animation);


    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout ptrFrameLayout) {
        mtv_refresh.setText("刷新完成!!!");
//        oa.cancel();


    }

    @Override
    public void onUIPositionChange(PtrFrameLayout ptrFrameLayout, boolean b, byte b1, PtrIndicator ptrIndicator) {


    }


}
